package com.kkshop.model;

import javax.persistence.CascadeType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.MappedSuperclass;
import javax.persistence.OneToOne;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

import com.kkshop.model.security.User;

@MappedSuperclass
public class AbstractUser implements Persistent<Long> {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "username")
    private User user;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public boolean equals(Object other) {
        if(!(other instanceof AbstractUser)) {
            return false;
        }
        
        AbstractUser that = (AbstractUser) other;
        
        return new EqualsBuilder()
                .append(id, that.id)
                .append(user, that.user).isEquals();
    }

    public int hashCode() {
        return new HashCodeBuilder()
                .append(id)
                .append(user).toHashCode();
    }

    public String toString() {
        return new ToStringBuilder(this)
                .append("id", id)
                .append("user", user).toString();
    }
}
